﻿namespace SERFLITEMAX20.Core
{
    using SERFLITEMAX20;
    using System;
    using System.Reflection;

    internal class PlayerNames : BaseHashtable
    {
        internal PlayerNames(Main owner) : base(owner)
        {
            base.basePtr = Addresses.STATIC_g_nameDBCache;
        }

        internal override object GetKey(int nPtr)
        {
            return base.owner.Memory.ReadLong(nPtr + 0x18);
        }

        internal override object GetValue(int nPtr)
        {
            PlayerName name = new PlayerName();
            name.ptr = nPtr;
            name.name = base.owner.Memory.ReadString(nPtr + Addresses.OFFSET_PLAYERNAME, Addresses.NAME_LENGTH);
            if (name.name == "")
            {
                name.name = null;
            }
            return name;
        }

        internal override object this[object key]
        {
            get
            {
                PlayerName name = (PlayerName) base.table[key];
                if (name == null)
                {
                    return null;
                }
                if (name.name == null)
                {
                    name.name = base.owner.Memory.ReadString(name.ptr + Addresses.OFFSET_PLAYERNAME, Addresses.NAME_LENGTH);
                    if (name.name == "")
                    {
                        name.name = null;
                    }
                }
                return name.name;
            }
        }

        private class PlayerName
        {
            internal string name;
            internal int ptr;
        }
    }
}

